package com.lidy.tankbattle.tankgame2.tank;

import com.lidy.tankbattle.tankgame2.constant.TankPriorities;
import com.lidy.tankbattle.tankgame2.panel.TankPanel;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.time.LocalTime;
import java.util.Random;

/**
 * @author lidy
 * @version V2.0
 * @classname EnemyTank
 * @description 敌方坦克
 * @date 2022/5/29 10:36
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class EnemyTank extends Tank {

    /**
     * 发射子弹间隔时间
     **/
    private int randomSeed;

    /**
     * 在一个方向上持续移动的距离
     **/
    private int remainderLength;

    /**
     * 控制坦克发射子弹
     **/
    private EnemyTankShot shot;

    public EnemyTank(int x, int y, int width, int height, int direction, int number, int bloodVolume,
                     int randomSeed, LocalTime createdTime, Boolean invincible) {
        super(x, y, width, height, TankPriorities.ENEMY_TANK_TYPE, direction, number, bloodVolume, createdTime, invincible);
        this.randomSeed = randomSeed;
        this.remainderLength = new Random(randomSeed).nextInt(TankPriorities.PANEL_HEIGHT - height) + getSpeed();
        this.shot = new EnemyTankShot();
    }

    public static EnemyTank getEnemyTank(int x, int y, int number, LocalTime createdTime, Boolean invincible) {
        return new EnemyTank(x, y, TankPriorities.ENEMY_TANK_WIDTH, TankPriorities.ENEMY_TANK_HEIGHT,
                TankPriorities.DIRECTION_DOWN, number + TankPriorities.ENEMY_TANK_START_NUMBER,
                TankPriorities.ENEMY_TANK_INIT_BLOOD_VOLUME, (int) (Math.random() * 100) + 10, createdTime, invincible);
    }

    /**
     * @author: lidy
     * @description: 坦克发射子弹线程
     * @date: 2022/6/3 18:07
     */
    class EnemyTankShot implements Runnable {
        @Override
        public void run() {
            while (TankPanel.gameContinue && getLive()) {
                try {
                    Random random = new Random(randomSeed);
                    int r = random.nextInt(500) + 1000;
                    Thread.sleep(r);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                shot();
            }
        }
    }
}
